package com.example.wangning.mycarofcountry.forum.jingxuan;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.example.mycarofcountry.entity.dao.*;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;
import com.example.wangning.mycarofcountry.db.DaoHelper;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 16/1/14.
 */
public class JingXuanFragemnt extends Fragment {
    private ListView listView;
    private VolleySingleton volleySingleton;
    private JingXuanAdapter adapter;
    private DaoHelper helper;
    private  String url = "http://api.budejie.com/api/api_open.php?market=taobao&maxid=&udid=359357052150266&a=newlist&appname=baisibudejie&c=data&os=4.3&client=android&userID=&page=1&per=20&sub_flag=1&visiting=&type=10&time=week&mac=50%3AFC%3A9F%3AFC%3A75%3A7E&ver=6.0.1";
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_jingxuan,null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.listview);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new JingXuanAdapter(getActivity());
        listView.setAdapter(adapter);
        helper = DaoHelper.getInstance();
        initData(url);
    }

    private void initData(String url) {
        volleySingleton = VolleySingleton.getVolleyIntance();
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                JingXuanBean jingXuanBean = gson.fromJson(response,JingXuanBean.class);
                if (jingXuanBean != null){
                    List<JingXuanList> jingXuanList = jingXuanBean.getList();
                    if (jingXuanList != null && jingXuanList.size() > 0){
                        adapter.addData(jingXuanList);
                        helper.addJingXuanList(jingXuanList);
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                adapter.addData(helper.seacherJingXuanList());
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString, HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        volleySingleton.addRequest(stringRequest);
    }
}
